package com.chanjx.myspringwebflux.controller;

import com.chanjx.myspringwebflux.handler.MyWebSocketHandler;
import io.netty.util.internal.ThreadLocalRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

/**
 * @author 陈俊雄
 * @date 2019/5/15
 **/
@RestController
public class MyController {
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @GetMapping("/test01")
    public String test01() {
        return "test01";
    }

    @GetMapping("/test02")
    public Mono<String> test02() {
        return Mono.just("test02");
    }

    @GetMapping("/random")
    public Flux<ServerSentEvent<Integer>> random() {
        return Flux.interval(Duration.ofSeconds(1))
                .map(seq -> ServerSentEvent.<Integer>builder()
                        .event("random")
                        .id(seq.toString())
                        .data(ThreadLocalRandom.current().nextInt())
                        .build()
                );
    }
}
